package com.ruoyi.mp.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.MpClientCard;
import com.ruoyi.db.domain.system.PcCard;
import com.ruoyi.db.domain.web.MpIncomeRecord;
import com.ruoyi.db.mapper.system.MpClientCardMapper;

import com.ruoyi.db.mapper.system.MpClientMapper;
import com.ruoyi.db.mapper.system.PcCardMapper;
import com.ruoyi.db.mapper.web.MpIncomeRecordMapper;
import com.ruoyi.mp.service.IMpClientCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 我的道具卡Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-05
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class MpClientCardServiceImpl implements IMpClientCardService
{
    @Autowired
    private MpClientCardMapper mpClientCardMapper;
    @Autowired
    private PcCardMapper pcCardMapper;
    @Autowired
    private MpClientMapper mpClientMapper;
    @Autowired
    private MpIncomeRecordMapper mpIncomeRecordMapper;


    /**
     * 查询我的道具卡
     * 
     * @param id 我的道具卡主键
     * @return 我的道具卡
     */
    @Override
    public MpClientCard selectMpClientCardById(Long id)
    {
        return mpClientCardMapper.selectMpClientCardById(id);
    }

    /**
     * 查询我的道具卡列表
     * 
     * @param mpClientCard 我的道具卡
     * @return 我的道具卡
     */
    @Override
    public List<MpClientCard> selectMpClientCardList(MpClientCard mpClientCard)
    {
        List<MpClientCard> cards = mpClientCardMapper.selectMpClientCardList(mpClientCard);
        List<MpClientCard> cardsR = new ArrayList<>();
        for (MpClientCard clientCard: cards) {
            PcCard pcCard = pcCardMapper.selectPcCardById(clientCard.getCardId());
            clientCard.setCardName(pcCard.getName());
            clientCard.setCardImg(pcCard.getImg());
            if(pcCard.getIsDelete()!=1){
                cardsR.add(clientCard);
            }
        }
        return cardsR;
    }

    /**
     * 新增我的道具卡
     * 
     * @param mpClientCard 我的道具卡
     * @return 结果
     */
    @Override
    public void insertMpClientCard(MpClientCard mpClientCard, MpClient client)
    {
        PcCard pcCard = pcCardMapper.selectPcCardById(mpClientCard.getCardId());
        if(pcCard.getSilverPrice().compareTo(client.getSilverBeanBalance())>0){
            throw new ServiceException("用户银藻余额不足");
        }
        client.setSilverBeanBalance(client.getSilverBeanBalance().subtract(pcCard.getSilverPrice()));
        mpClientMapper.updateMpClient(client);
        mpClientCard.setCardCount(1l);
        MpIncomeRecord mpIncomeRecord = new MpIncomeRecord();
        mpIncomeRecord.setClientId(client.getClientId());
        mpIncomeRecord.setIsIncome(0l);
        mpIncomeRecord.setType(2l);
        mpIncomeRecord.setAmount(pcCard.getSilverPrice());
        mpIncomeRecord.setCreateTime(new Date());
        mpIncomeRecord.setContent("用户购买"+pcCard.getName());
        mpIncomeRecord.setCurrentBalance(client.getSilverBeanBalance());
        mpIncomeRecordMapper.insertMpIncomeRecord(mpIncomeRecord);
        MpClientCard mpClientCardOld = new MpClientCard();
        mpClientCardOld.setClientId(client.getClientId());
        mpClientCardOld.setCardId(mpClientCard.getCardId());
        List<MpClientCard> list = mpClientCardMapper.selectMpClientCardList(mpClientCardOld);
        if(list.size()>0){
            MpClientCard myCard = list.get(0);
            myCard.setCardCount(myCard.getCardCount()+1);
            mpClientCardMapper.updateMpClientCard(myCard);
        }else{
            mpClientCardMapper.insertMpClientCard(mpClientCard);
        }
    }

    /**
     * 修改我的道具卡
     * 
     * @param mpClientCard 我的道具卡
     * @return 结果
     */
    @Override
    public int updateMpClientCard(MpClientCard mpClientCard)
    {
        return mpClientCardMapper.updateMpClientCard(mpClientCard);
    }

    /**
     * 批量删除我的道具卡
     * 
     * @param ids 需要删除的我的道具卡主键
     * @return 结果
     */
    @Override
    public int deleteMpClientCardByIds(Long[] ids)
    {
        return mpClientCardMapper.deleteMpClientCardByIds(ids);
    }

    /**
     * 删除我的道具卡信息
     * 
     * @param id 我的道具卡主键
     * @return 结果
     */
    @Override
    public int deleteMpClientCardById(Long id)
    {
        return mpClientCardMapper.deleteMpClientCardById(id);
    }

    @Override
    public List<PcCard> selectPcCardList() {
        return pcCardMapper.selectPcCardList(new PcCard());
    }
}
